package com.dayu.store.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.dayu.common.utils.DateUtils;
import com.dayu.common.utils.StringUtils;
import com.dayu.store.domain.AppletClerkUser;
import com.dayu.store.mapper.AppletClerkUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dayu.store.mapper.AppletCompanyStoreMapper;
import com.dayu.store.domain.AppletCompanyStore;
import com.dayu.store.service.IAppletCompanyStoreService;
import org.springframework.util.ObjectUtils;

/**
 * 公司门店Service业务层处理
 * 
 * @author HK
 * @date 2022-01-07
 */
@Service
public class AppletCompanyStoreServiceImpl implements IAppletCompanyStoreService 
{
    @Autowired
    private AppletCompanyStoreMapper appletCompanyStoreMapper;
    @Autowired
    private AppletClerkUserMapper appletClerkUserMapper;

    /**
     * 查询公司门店
     * 
     * @param id 公司门店ID
     * @return 公司门店
     */
    @Override
    public AppletCompanyStore selectAppletCompanyStoreById(Long id)
    {
        return appletCompanyStoreMapper.selectAppletCompanyStoreById(id);
    }

    /**
     * 查询公司门店列表
     * 
     * @param appletCompanyStore 公司门店
     * @return 公司门店
     */
    @Override
    public List<AppletCompanyStore> selectAppletCompanyStoreList(AppletCompanyStore appletCompanyStore)
    {
        return appletCompanyStoreMapper.selectAppletCompanyStoreList(appletCompanyStore);
    }

    /**
     * 新增公司门店
     * 
     * @param appletCompanyStore 公司门店
     * @return 结果
     */
    @Override
    public int insertAppletCompanyStore(AppletCompanyStore appletCompanyStore)
    {
        checkParams(appletCompanyStore);
        appletCompanyStore.setCreateTime(DateUtils.getNowDate());
        return appletCompanyStoreMapper.insertAppletCompanyStore(appletCompanyStore);
    }

    /**
     * 修改公司门店
     * 
     * @param appletCompanyStore 公司门店
     * @return 结果
     */
    @Override
    public int updateAppletCompanyStore(AppletCompanyStore appletCompanyStore)
    {
        checkParams(appletCompanyStore);
        appletCompanyStore.setUpdateTime(DateUtils.getNowDate());
        return appletCompanyStoreMapper.updateAppletCompanyStore(appletCompanyStore);
    }

    /**
     * 批量删除公司门店
     * 
     * @param ids 需要删除的公司门店ID
     * @return 结果
     */
    @Override
    public int deleteAppletCompanyStoreByIds(Long[] ids)
    {
        for(int i = 0; i < ids.length; i++) {
            AppletCompanyStore store = selectAppletCompanyStoreById(ids[i]);
            AppletCompanyStore selStore = new AppletCompanyStore();
            selStore.setParentId(store.getId());
            List<AppletCompanyStore> selStoreList = selectAppletCompanyStoreList(selStore);
            if(selStoreList.size() > 0) {
                throw new RuntimeException("请先删除下级！");
            }
            AppletClerkUser clerkUser = new AppletClerkUser();
            if(store.getParentId() == 0) {
                clerkUser.setStoreId("["+ store.getId() +"]");
            }else {
                clerkUser.setStoreId("["+ store.getParentId() +","+ store.getId() +"]");
            }
            List<AppletClerkUser> selUserList = appletClerkUserMapper.selectAppletClerkUserList(clerkUser);
            if(selUserList.size() > 0) {
                throw new RuntimeException("请先删除门店下的用户！");
            }
        }
        return appletCompanyStoreMapper.deleteAppletCompanyStoreByIds(ids);
    }

    /**
     * 删除公司门店信息
     * 
     * @param id 公司门店ID
     * @return 结果
     */
    @Override
    public int deleteAppletCompanyStoreById(Long id)
    {
        return appletCompanyStoreMapper.deleteAppletCompanyStoreById(id);
    }

    private void checkParams(AppletCompanyStore companyStore) {
        if(ObjectUtils.isEmpty(companyStore.getName())) {
            throw  new RuntimeException("名称不能为空");
        }
        if(ObjectUtils.isEmpty(companyStore.getParentId())) {
            throw new RuntimeException("上级不能为空");
        }
        if(companyStore.getParentId().equals(companyStore.getId())) {
            throw new RuntimeException("上级不能是自己");
        }
        if(StringUtils.isNotEmpty(companyStore.getUserId())) {
            AppletCompanyStore selStore = new AppletCompanyStore();
            selStore.setUserId(companyStore.getUserId());
            List<AppletCompanyStore> selList = selectAppletCompanyStoreList(selStore);
            List<AppletCompanyStore> fList = selList.stream().filter(it -> !it.getId().equals(companyStore.getId()) ).collect(Collectors.toList());

            if(fList.size() > 0) {
                throw new RuntimeException("该手机号已存在！");
            }
        }

    }
}
